package com.iweb.userOrder.servlet;

import com.alibaba.fastjson.JSONObject;
import com.iweb.userOrder.model.Orders;
import com.iweb.userOrder.service.OrdersService;
import com.iweb.userOrder.service.OrdersServiceImpl;
import com.iweb.util.PageModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(value = "*.orders")
public class OrdersServlet extends HttpServlet {

    OrdersService ordersService = new OrdersServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String path = req.getServletPath();
        switch (path) {
            case "/toList.orders":
                toList(req, resp);
                break;
            case "/selectByPage.orders":
                selectByPage(req, resp);
                break;
            case "/toEdit.orders":
                toEdit(req,resp);
                break;
            case "/addOrUpdate.orders":
                addOrUpdate(req,resp);
                break;
            case "/queryById.orders":
                queryById(req,resp);
                break;
            case "/deleteById.orders":
                deleteById(req,resp);
                break;
            default:
                break;
        }
    }

    /*
根据id删除一条记录
 */
    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String order_id = req.getParameter("order_id");
        ordersService.deleteById(order_id);
        resp.sendRedirect("toList.orders?pageNo=1&likeValue=");
    }

    /*
根据id查询一条记录
 */
    private void queryById(HttpServletRequest req, HttpServletResponse resp) throws  ServletException,IOException {
        String order_id = req.getParameter("order_id");
        System.out.println("查询订单ID: " + order_id);
        Orders orders = ordersService.queryById(order_id);
        System.out.println("查询结果: " + orders);
        resp.setContentType("application/json;charset=utf-8");
        String result = JSONObject.toJSONString(orders);
        System.out.println("返回JSON: " + result);
        resp.getWriter().print(result);
    }

    /*
 新增或修改
  */
    private void addOrUpdate(HttpServletRequest req, HttpServletResponse resq) throws ServletException,IOException {
        System.out.println("=== 开始处理订单提交 ===");
        String dish_name = req.getParameter("dish_name");
        String order_id = req.getParameter("order_id");
        String dish_price = req.getParameter("dish_price");
        
        System.out.println("接收到的参数：");
        System.out.println("dish_name: " + dish_name);
        System.out.println("order_id: " + order_id);
        System.out.println("dish_price: " + dish_price);
        
        if (order_id == null || "".equals(order_id)) {
            //新增
            System.out.println("执行新增操作");
            try {
                Orders orders = new Orders(null,dish_name,Integer.valueOf(dish_price),null);
                System.out.println("准备新增的订单对象：" + orders);
                int result = ordersService.addOne(orders);
                System.out.println("新增结果：" + result);
                if(result > 0) {
                    System.out.println("新增成功");
                } else {
                    System.out.println("新增失败");
                }
            } catch (Exception e) {
                System.out.println("新增过程中发生异常：");
                e.printStackTrace();
            }
        } else {
            //修改
            System.out.println("执行修改操作");
            try {
                Orders orders = new Orders(Integer.valueOf(order_id),dish_name,Integer.valueOf(dish_price),null);
                System.out.println("准备修改的订单对象：" + orders);
                int result = ordersService.updateOne(orders);
                System.out.println("修改结果：" + result);
                if(result > 0) {
                    System.out.println("修改成功");
                } else {
                    System.out.println("修改失败");
                }
            } catch (Exception e) {
                System.out.println("修改过程中发生异常：");
                e.printStackTrace();
            }
        }
        System.out.println("=== 订单处理完成 ===");
        resq.sendRedirect("toList.orders?pageNo=1&likeValue=");
    }
    /*
跳转去编辑页面
 */
    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String order_id = req.getParameter("order_id");
        resp.sendRedirect("orders/orders_toAdd.html?order_id=" + order_id);
    }

    /*
    分页查询
     */
    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageNo = req.getParameter("pageNo");
        int pageNoInt = Integer.parseInt(pageNo);
        String likeValue = req.getParameter("likeValue");
        PageModel pageModel = new PageModel();
        pageModel.setPageNo(pageNoInt);
        pageModel.setLikeValue(likeValue);
        List<Orders> orders = ordersService.selectByPage(pageModel);
        pageModel.setRecordList(orders);
        String result = JSONObject.toJSONString(pageModel);
        resp.getWriter().print(result);
    }

    /*
    跳转去查询页面
     */
    private void toList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/orders/orders_selectByPage.html")
                .forward(req, resp);
    }
}
